
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    model: Object,
    user: Object,
    contestant: Object,
    firstTitle: Object,
    secondTitle: Object,
    thirdTitle: Object,
  },

  /**
   * 组件的初始数据
   */
  data: {
    open: false,
  },

  // // 修改properties.user时完成
  // observers: {
  //   'user': function (user) {//  'user'是要监听的字段，（user）是已更新变化后的数据
  //     this.setData({
  //       userdata: user //这里user不能赋值给user，不然就会陷入死循环，导致内存占用过高，开发者工具死机。
  //     })
  //   },
  // },

  /**
   * 组件的方法列表
   */
  methods: {
    toggle: function (e) {
      this.setData({
        open: !this.data.open
      })
    },

    // 子组件向父组件传参
    onMyEvent: function (e) {
      // console.log(e.detail.index, e.detail.title)
      if(e.detail.index == 2) {
        this.properties.secondTitle = e.detail.title
        this.properties.thirdTitle = e.detail.thirdTitle
        
        // 如果是从第三级跳转过来，则跳转到代码页面
        // console.log(e.detail.fromLevel)
        if (e.detail.fromLevel == 3) {
          let that = this
          wx.cloud.callFunction({
            name: "getCodeByTitleAndConId",
            data: {
              contestant: that.properties.contestant,
              firstTitle: that.properties.firstTitle,
              secondTitle: that.properties.secondTitle,
              thirdTitle: that.properties.thirdTitle,
            },
            success: res => {
              console.log("调用云函数获取代码成功", res)
              wx.navigateTo({
                url: '/pages/showCode/showCode?firstTitle=' + that.properties.firstTitle + "&secondTitle=" + that.properties.secondTitle + "&thirdTitle=" + that.properties.thirdTitle + "&code=" + res.result.data[0].code
              })
            },
            fail: res => {
              console.log("调用云函数获取代码失败", res)
            }
          })
        }
      } else if(e.detail.index == 3) {
        this.properties.thirdTitle = e.detail.title
        let item = { index: 2, title: this.properties.secondTitle, thirdTitle: this.properties.thirdTitle, fromLevel: 3}
        this.triggerEvent('myevent', item);
      }
    },

    // 点击跳转到代码
    showCode: function (e) {
      let that = this
      // console.log(that.properties.user)
      // console.log(e.currentTarget.dataset.title)
      // console.log(e.currentTarget.dataset.index)
      if (e.currentTarget.dataset.index == 1) {
        that.properties.firstTitle = e.currentTarget.dataset.title
        // 根据用户openId获得所有报名记录
        wx.cloud.callFunction({
          name: "getSignUpRecordByOpenId",
          data: {
            openId: that.properties.user.openId
          },
          success: res => {
            console.log("调用云函数获取报名记录成功", res)
            let signUpList = res.result.data
            // 根据报名记录获得考号
            for(let i = 0; i < signUpList.length; ++i) {
              if(signUpList[i].gameTitle == e.currentTarget.dataset.title) {
                that.properties.contestant = signUpList[i].contestant_id
                break;
              }
            }
          },
          fail: res => {
            console.log("调用云函数获取报名记录失败", res)
          }
        })
        // console.log(that.properties.contestant)
        // console.log(that.properties.firstTitle)
        // console.log(that.properties.secondTitle)
        // console.log(that.properties.thirdTitle)
        
      } else if (e.currentTarget.dataset.index == 2) {
        this.properties.secondTitle = e.currentTarget.dataset.title
        let item = { index: 2, title: e.currentTarget.dataset.title, thirdTitle: this.properties.thirdTitle}
        this.triggerEvent('myevent', item);
        // that.properties.secondTitle = e.currentTarget.dataset.title
        // console.log(that.properties.contestant)
        // console.log(that.properties.firstTitle)
        // console.log(that.properties.secondTitle)
        // console.log(that.properties.thirdTitle)
      } else if (e.currentTarget.dataset.index == 3) {
        this.properties.thirdTitle = e.currentTarget.dataset.title
        let item = { index: 3, title: e.currentTarget.dataset.title }
        this.triggerEvent('myevent', item);
        // that.properties.thirdTitle = e.currentTarget.dataset.title
        // console.log(that.properties.contestant)
        // console.log(that.properties.firstTitle)
        // console.log(that.properties.secondTitle)
        // console.log(that.properties.thirdTitle)
      }
    },
  }
})

